package org.karelun.gef.property;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.ComboBoxLabelProvider;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.karelun.gef.model.Node;

public class NodeComboBoxPropertyDescriptor extends PropertyDescriptor {

	private List<Node> nodes;

	public NodeComboBoxPropertyDescriptor(Object id, String displayName,
			List<Node> nodes) {
		super(id, displayName);
		this.nodes = nodes;
	}

	public CellEditor createPropertyEditor(Composite parent) {
		String[] labels=getLabels().toArray(new String[0]);
		CellEditor editor = new ComboBoxCellEditor(parent, labels,
				SWT.READ_ONLY);
		if (getValidator() != null) {
			editor.setValidator(getValidator());
		}
		return editor;
	}

	public ILabelProvider getLabelProvider() {
		if (isLabelProviderSet()) {
			return super.getLabelProvider();
		}
		String[] labels=getLabels().toArray(new String[0]);
		return new ComboBoxLabelProvider(labels);
	}
	
	private List<String> getLabels(){
		List<String> labels = new ArrayList<String>(nodes.size());
		for(Node node:nodes){
			String name = node.getText();
			labels.add(name);
		}
		return labels;
	}
}
